package com.softer.chengxin.report.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.util.DateFormatUtil;
import com.softer.chengxin.corp.dao.CorpDao;
import com.softer.chengxin.report.dao.ReportPrintDao;
import com.softer.chengxin.report.entity.Report;
import com.softer.chengxin.report.service.ReportPrintService;
import com.softer.chengxin.score.entity.ScoreCompositeProvince;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2020/1/18
 */
@Service
public class ReportPrintServiceImpl implements ReportPrintService {
    @Autowired
    private ReportPrintDao reportDao;
    @Autowired
    private CorpDao corpDao;

    /**
     * 查询信用报告列表
     *
     * @param scoreCompositeProvince 查询实体
     * @param page                   分页实体
     * @param loginName              登录人
     * @return 列表
     */
    @Override
    public PageInfo<ScoreCompositeProvince> getReportList(ScoreCompositeProvince scoreCompositeProvince, Page page, String loginName) {
        setScoreComposite(loginName, scoreCompositeProvince);
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<ScoreCompositeProvince> list = reportDao.getReportList(scoreCompositeProvince);
        return new PageInfo<>(list);
    }

    /**
     * 查询信用报告
     *
     * @param id 信用ID
     * @return 信用报告
     */
    @Override
    public Report getReportById(Integer id) {
        return reportDao.getReportById(id);
    }

    /**
     * 设置登录人的管理区域
     *
     * @param loginName              登录人
     * @param scoreCompositeProvince 查询实体
     */
    private void setScoreComposite(String loginName, ScoreCompositeProvince scoreCompositeProvince) {
        String area = null;
        if (!Common.ADMIN.equals(loginName)) {
            area = corpDao.getDistinct(loginName);
        }
        if (StringUtils.isEmpty(scoreCompositeProvince.getYear())) {
            scoreCompositeProvince.setYear(DateFormatUtil.getSysYear());
        }
        scoreCompositeProvince.setArea(area);
    }
}
